package com.cl.short_video_java.service.impl;

import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cl.short_video_java.pojo.SysFastOper;
import com.cl.short_video_java.service.*;
import com.cl.short_video_java.mapper.SysFastOperMapper;
import com.cl.short_video_java.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author object
 * @description 针对表【sys_fast_oper】的数据库操作Service实现
 * @createDate 2024-11-03 12:56:48
 */
@Service
public class SysFastOperServiceImpl extends ServiceImpl<SysFastOperMapper, SysFastOper>
		implements SysFastOperService {

	private SysSettingService sysSettingService;
	private SysFastOperMapper sysFastOperMapper;
	private VideoShopOrderService videoShopOrderService;
	private VideoVideoService videoVideoService;
	private VideoUserService videoUserService;
	private VideoTypeService videoTypeService;

	@Autowired
	public SysFastOperServiceImpl(SysSettingService sysSettingService,
								  SysFastOperMapper sysFastOperMapper,
								  VideoShopOrderService videoShopOrderService,
								  VideoVideoService videoVideoService,
								  VideoUserService videoUserService,
								  VideoTypeService videoTypeService) {
		this.sysSettingService = sysSettingService;
		this.sysFastOperMapper = sysFastOperMapper;
		this.videoShopOrderService = videoShopOrderService;
		this.videoVideoService = videoVideoService;
		this.videoUserService = videoUserService;
		this.videoTypeService = videoTypeService;
	}

	/**
	 * 获取在启用快捷操作
	 *
	 * @return
	 */
	@Override
	public Map<String, List<SysFastOper>> getList() {
		String sysFasterCount = sysSettingService.getSysSettingByKey("sys_faster_count");
		String sysPreviewCount = sysSettingService.getSysSettingByKey("sys_preview_count");
		HashMap<String, List<SysFastOper>> map = new HashMap<>();
		List<SysFastOper> fastOpers = this.list(new QueryWrapper<SysFastOper>()
				.eq("status", 1)
				.eq("is_delete", 0)
				.eq("type", 1)
				.last("limit " + sysFasterCount));
		List<SysFastOper> previews = this.list(new QueryWrapper<SysFastOper>()
				.eq("status", 1)
				.eq("is_delete", 0)
				.eq("type", 2)
				.last("limit " + sysPreviewCount));

		for (SysFastOper sysFastOper : previews) {
			Long count = 0L;
			switch (sysFastOper.getKey()) {
				case "video":
					count = videoVideoService.count();
					break;
				case "order":
					count = videoShopOrderService.count();
					break;
				case "user":
					count = videoUserService.count();
					break;
				case "type":
					count = videoTypeService.count();
					break;
				default:
					count = 0L;
					break;
			}
			sysFastOper.setCount(count);
		}
		map.put("fast", fastOpers);
		map.put("preview", previews);
		return map;
	}

	@Override
	public void save(Integer id, Integer type) {
		SysFastOper fastOper = this.getById(id);
		Assert.isTrue(StringUtils.isNotEmpty(fastOper.getTitle()), "当前项为空");
		if (type == 1) {
			// 停用-启用
			fastOper.setStatus(fastOper.getStatus() == 1 ? 0 : 1);
		} else {
			// 删除
			fastOper.setIsDelete(1);
		}

		this.saveOrUpdate(fastOper);
	}
}




